package com.rabc.web.controller;

import com.core.base.BaseController;
import com.rabc.web.entity.SysMenu;
import com.rabc.web.service.SysMenuService;

import com.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("查询菜单列表")
    @GetMapping("/list")
    public R list(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuService.selectMenuList(sysMenu);
        return R.success(list);
    }


    @ApiOperation("查询树形菜单列表")
    @GetMapping("/treeDate")
    public R treeDate(@Param("roleId") String roleId) {
        List<SysMenu> list = sysMenuService.selectMenuTree(null);
        List<String> menuIds = sysMenuService.selectMenuListByRoleId(roleId);
        Map<String,Object> map= new HashMap<>();
        map.put("menus",list);
        map.put("checkedKeys",menuIds);
        return R.success(map).data("menus",list);
    }


    @ApiOperation("根据ID查询菜单")
    @GetMapping("/{menuId}")
    public R getInfo(@PathVariable String menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return R.success(sysMenu);
    }

    @ApiOperation("新增菜单")
    @PostMapping
    public R add(@RequestBody SysMenu sysMenu) {
        return sysMenuService.save(sysMenu)?R.success():R.error();
    }

    @ApiOperation("修改菜单")
    @PutMapping
    public R edit(@RequestBody SysMenu sysMenu) {
        return sysMenuService.updateById(sysMenu)?R.success():R.error();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{menuId}")
    public R remove(@PathVariable String menuId) {
        return sysMenuService.removeById(menuId)?R.success():R.error();
    }



}
